
import json
import os
import sys
import copy
import commands
import codecs




def email(mail_list,title,table_head,data,template_file,subject,module='offline_platform_log_miss'):
    status,output = commands.getstatusoutput('date -d-1day "+%Y%m%d"')
    filename = "../data/%s_%s"%(module,output)
    f = codecs.open(filename, 'w', "gbk")
    from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache
    env = Environment(loader = FileSystemLoader('../template/'),
                    bytecode_cache = FileSystemBytecodeCache('../template/', '%s.cache'))
    tmpl_jinja = env.get_template(template_file)
    template = tmpl_jinja.render(title=title,table_head=table_head,data=data)
    print >>f,template
    f.close()
    (status,output) = commands.getstatusoutput('cat %s |formail -I "From: lbsstat-tools@baidu.com" -I "To:%s"  -I "MIME-Version:1.0" -I "Content-type:text/html;charset=gb2312" -I "Subject:%s"|/usr/sbin/sendmail  -oi %s'%(filename,mail_list,subject,mail_list))
    #(status, output) = commands.getstatusoutput('cat %s | mail -s "%s" %s'%(filename,os.path.basename(self.parameter[2]),mail_list))
    print output
    return status

